﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SignInServer.Server.Configuration;
using SignInServer.Server.Tools;

namespace SignInServer.Server
{
    /// <summary>
    /// 储存客户端发送的数据，并拆解
    /// </summary>
    class UnpackMessage
    {
        /// <summary>
        /// 原始数据链表
        /// </summary>
        public LinkedList<byte> initialDataList;
        /// <summary>
        /// 原始数据起始位置
        /// </summary>
        public int initalDataStartIndex;
        /// <summary>
        ///  原始数据长度(提升访问速度)
        /// </summary>
        public int initialDataCount;

        public UnpackMessage()
        {
            initalDataStartIndex = 0;
            initialDataCount = 0;
            initialDataList = new LinkedList<byte>();
        }
        /// <summary>
        /// 添加数据到初始数据链表
        /// </summary>
        /// <param name="data"></param>
        public void AddInitialData(byte[] data)
        {
            int size = data.Length;
            for (int i = 0; i < size; i++)
            {
                initialDataList.AddLast(data[i]);
            }
            initialDataCount += size;
        }
        /// <summary>
        /// 处理初始数据链表数据
        /// </summary>
        /// <returns>拆解到的一个消息包</returns>
        public BasicMessage processingInitialData()
        {
            if (initialDataCount < 1)
            {
                Console.WriteLine("原始数据列表为空");
                return null;
            }

            if (initalDataStartIndex < initialDataCount)
            {
                if (initialDataList.ElementAt(initalDataStartIndex) == SocketConfiguration.DATA_START_TAG)
                {
                    if (initalDataStartIndex + 5 <= initialDataCount)
                    {
                        Console.WriteLine("起始符正确");
                        int packetSize = ConvertTypeTool.ByteArrayToInt32(SubList((initalDataStartIndex + 1), 4));
                        Console.WriteLine("包头记录：" + packetSize);
                        if (initialDataCount >= packetSize + 5 + initalDataStartIndex)
                        {
                            Console.WriteLine("数据为完整数据");
                            byte[] packetBytes = SubList((initalDataStartIndex + 5), packetSize);
                            if (packetBytes != null)
                            {
                                BasicMessage basicMessage = BasicMessage.disassembleMessage(packetBytes);
                                if (initialDataList.ElementAt(initalDataStartIndex + 5 + packetSize) == SocketConfiguration.DATA_END_TAG)
                                {
                                    initalDataStartIndex = initalDataStartIndex + 6 + packetSize;
                                    initialDataCleansing();
                                    return basicMessage;
                                }
                            }
                        }
                    }
                }
            }
            return null;
        }
        /// <summary>
        /// 清理原始数据链表
        /// </summary>
        public void initialDataCleansing()
        {
            Console.WriteLine("清理原始数据列表");
            for (int i = 0; i < initalDataStartIndex; i++)
            {
                initialDataList.RemoveFirst();
            }
            initialDataCount -= initalDataStartIndex;
            initalDataStartIndex = 0;
        }
        /// <summary>
        /// 剪切原始数据链表中的一段
        /// </summary>
        /// <param name="Start"></param>
        /// <param name="Count"></param>
        /// <returns></returns>
        public byte[] SubList(int Start, int Count)
        {
            byte[] sub = new byte[Count];

            for (int i = 0; i < Count; i++)
            {
                if (initialDataList.Count < i + Start)
                {
                    return null;
                }
                sub[i] = initialDataList.ElementAt(i + Start);
            }
            return sub;
        }
    }
}
